package com.shuyihui.identity

import android.Manifest
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.owbtgad.common.base.BaseDActivity
import com.owbtgad.common.network.net.IStateObserver
import com.owbtgad.common.utils.ToastUtils
import com.shuyihui.identity.databinding.IdeActivityCertificationBinding
import kotlinx.android.synthetic.main.ide_activity_certification.*
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.net.URLConnection
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.*
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager

class CertificationActivity : BaseDActivity<IdeActivityCertificationBinding>() {

    private val mViewModel :CertificationViewModel by viewModel()
    companion object {
        fun start(context: Context) {
            val intent = Intent(context, CertificationActivity::class.java)
            context.startActivity(intent)
        }
    }
    override fun getContentLayout(): Int {
        return R.layout.ide_activity_certification
    }

    override fun initView(savedInstanceState: Bundle?) {
        fragments = ArrayList()
        mBinding!!.slider.setOnTouchListener { _, _ -> true }
        mBinding!!.toolbar.setNavigationOnClickListener {
            finish()
        }

        mViewModel?.netLiveData?.observe(this, object : IStateObserver<String>(null) {

            override fun onDataChange(data: String?) {
                super.onDataChange(data)
                Log.d(TAG, "onDataChange: $data")
            }

            override fun onError(e: Throwable?) {
                super.onError(e)
                Log.e(TAG, "onError: ", e)
            }

            override fun onFail(code: Int?, msg: String?) {
                super.onFail(code, msg)
                msg?.also {
                    ToastUtils.showToast(mContext, msg)
                }
            }
        })
//        handleSSLHandshake()
//        mViewModel.getAuthConfig()
        val requestManager = Glide.with(mContext!!)
        val path="https://upload-images.jianshu.io/upload_images/23089205-d11cdeef229d32e7?imageMogr2/auto-orient/strip|imageView2/2/w/640/format/webp"
        requestManager.asBitmap().load(path).apply(RequestOptions()).into(object :
            SimpleTarget<Bitmap?>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
                iv_1.setImageBitmap(resource)
                /*val parts: MutableList<MultipartBody.Part> = ArrayList()
                parts.add(MultipartBody.Part.createFormData("type", "4"))
//                val requestBody = RequestBodyUtils.create("image/jpeg".toMediaTypeOrNull(), isBm)
                val paname = System.currentTimeMillis().toString() + ".jpg"
                val requestBody = RequestBodyUtils.create(
                    guessMimeType(paname),
                    isBm
                )
                val part = MultipartBody.Part.createFormData("file", paname, requestBody)
                parts.add(part)
                mViewModel.upload(parts)*/
//                mViewModel.authCommonUploadAd(resource, "4")

            }
        })

    }

    private var fragments: MutableList<Fragment?>? = null
    private var mPoint = 0
    private var mCardType: CardType? = null
    private val mAdPermissions = arrayOf(Manifest.permission.CAMERA)

    private fun guessMimeType(path: String): MediaType? {
        var path = path
        val fileNameMap = URLConnection.getFileNameMap()
        path = path.replace("#", "") //解决文件名中含有#号异常的问题
        var contentType = fileNameMap.getContentTypeFor(path)
        if (contentType == null) {
            contentType = "application/octet-stream"
        }
        return contentType.toMediaTypeOrNull()
    }

}